package com.motong.gongdan.service;

import com.motong.gongdan.util.ExcelTemplateUtil;
import com.motong.gongdan.vo.system.ExcelTemplate.vo.ExcelTemplateDataVo;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

@Service
public class CommonService {

    /**
     * 写出excel数据
     */

    public void exportExcelData(HttpServletResponse response, String templatePath, String templateName, List<ExcelTemplateDataVo> excelData) {
        try (OutputStream outputStream = response.getOutputStream();
             InputStream sourceInputStream = new FileInputStream(templatePath);
        ) {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(templateName, "UTF-8"));
            ExcelTemplateUtil excelTemplateUtil = new ExcelTemplateUtil();
            excelTemplateUtil.exportExcelByTemplate(sourceInputStream, excelData, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
